package com.hake.example.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.github.pagehelper.PageInfo;
import com.hake.core.exception.AppException;
import com.hake.core.support.HttpCode;
import com.hake.core.support.web.BaseController;
import com.hake.example.entity.User;
import com.hake.example.service.UserInfoService;
import com.hake.example.web.dto.UserInfo;

/**
 * 用户信息处理
 * @author yhaoquan
 *
 */
@Controller
@RequestMapping("/userinfo")
public class UserInfoController extends BaseController {

	@Autowired
	private UserInfoService userInfoService ;
	
	@GetMapping(value="main")
	public String main() {
		return viewName("main") ; 
	}
	
	@GetMapping(value="insert")
	public String insert(HttpServletRequest request, ModelMap modelMap) {
		return viewName("isnert") ;
	}
	
	@GetMapping(value="update")
	public String update(HttpServletRequest request, ModelMap modelMap) {
		return viewName("update") ;
	}
	
	
	@PostMapping(value = "insert")
	public Object insert(@RequestBody UserInfo dto, ModelMap modelMap) {
		boolean insert = this.userInfoService.insert(dto) ;
		if(insert) {
			return setModelMap(modelMap, HttpCode.OK) ;
		} else {
			return setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR) ;
		}
	}
	
	@DeleteMapping(value = "delete/{id}")
	public Object delete(@PathVariable Integer id, ModelMap modelMap) {
		return null ;
	}
	
	@PostMapping(value = "update")
	public Object update(UserInfo dto, HttpServletRequest request, ModelMap modelMap) {
		return null ;
	}
	
	@GetMapping(value = "get/{id}")
	public Object get(HttpServletRequest request, ModelMap modelMap, @PathVariable Integer id) {
		return null ;
	}
	
	@RequestMapping(value = "selectList")
	public @ResponseBody Object selectList(UserInfo dto, HttpServletRequest request, ModelMap modelMap) {
		try {
			modelMap.clear();
			
			List<User> selectList = this.userInfoService.selectList(dto) ;
			return setSuccessModelMap(modelMap, selectList) ;
			
		} catch (AppException e) {
			return setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR) ;
		}
	}
	
	@RequestMapping(value = "selectPage")
	public Object selectPage(HttpServletRequest request, ModelMap modelMap, UserInfo dto) {
		try {
			modelMap.clear();
			PageInfo<User> selectPage = this.userInfoService.selectPage(dto) ;
			System.out.println(selectPage);
			return setSuccessModelMap(modelMap, selectPage) ;
		} catch (AppException e) {
			return setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR) ;
		}
	}
	
	@RequestMapping(value = "selectPageParams")
	public Object selectPageParams(HttpServletRequest request, ModelMap modelMap) {
		try {
			System.out.println("==缓存1==");
			Map<String, Object> parametersStartingWith = WebUtils.getParametersStartingWith(request, null) ;
			PageInfo<User> selectPage = this.userInfoService.selectPage(parametersStartingWith) ;
			System.out.println("==缓存2=="+parametersStartingWith);
			return setSuccessModelMap(modelMap, selectPage) ;
		} catch (AppException e) {
			return setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR) ;
		}
	}
	
}
